package com.weixing.mall.provider.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.weixing.mall.provider.mapper.CategorySpecMapper;
import com.weixing.mall.provider.mapper.SpecValueMapper;
import com.weixing.mall.provider.model.domain.CategorySpec;
import com.weixing.mall.provider.model.dto.CategorySpecDto;
import com.weixing.mall.provider.model.query.CategorySpecQuery;
import com.weixing.mall.provider.model.query.SpecValueQuery;
import com.weixing.mall.provider.model.vo.CategorySpecVo;
import com.weixing.mall.provider.model.vo.SpecValueVo;
import com.weixing.mall.provider.service.ICategorySpecService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @ClassName CategroySpecServiceImpl
 * @Description TODO <br/>
 * @Author gjq
 * @Modified By:
 * @Date 2019/8/12
 * @Version 1.0
 **/
/**
 * <p>
 * 商品模块-商品类目规格关联表 服务实现类
 * </p>
 *
 * @author Small
 * @since 2019-11-20
 */
@Service
public class CategroySpecServiceImpl extends ServiceImpl<CategorySpecMapper, CategorySpec> implements ICategorySpecService {

    @Autowired
    private SpecValueMapper specValueMapper;

    @Override
    public void save(CategorySpecDto categorySpecDto) {
        CategorySpec categorySpec = new CategorySpec();
        BeanUtils.copyProperties(categorySpecDto,categorySpec);
        if (null == categorySpecDto.getId()){
            save(categorySpec);
        }else{
            updateById(categorySpec);
        }
    }

    @Override
    public List<CategorySpecVo> selectList(CategorySpecQuery categorySpecQuery) {
        List<CategorySpecVo> list = baseMapper.selectList(categorySpecQuery);
        if(list.size()>0){
            //规格list里面每个规格添加  规格值list
            for(int i=0;i<list.size();i++){
                //每个规格
                CategorySpecVo categorySpecVo = list.get(i);
                //查询规格值list
                SpecValueQuery specValueQuery = new SpecValueQuery();
                specValueQuery.setSpecId(categorySpecVo.getSpecId());
                List<SpecValueVo> specValueVoList = specValueMapper.selectList(specValueQuery);
                //规格值list添加进 对象
                categorySpecVo.setSpecList(specValueVoList);
                list.set(i,categorySpecVo);
            }
        }

        return list;
    }
}
